<?php
/**
 * Created by IntelliJ IDEA.
 * User: HXL
 * Date: 2018/9/29
 * Time: 17:20
 */
namespace app\order\controller;

use think\Lang;

class Supply extends Base{
    /**
     * 列表页
     */
    public function index(){
        $field = 'sup_id,sup_type,mod_id,provider,sup_createtime,fit_id';
        $supplies = $this->supplies()->page();
        $supplies = $this->resetSupplies($supplies);
        $this->page($supplies);
        $this->asgModelTypes();
        $this->asgCiuFits();
        $this->assign('supplies', $supplies);
        return $this->fetch('sup/index');
    }

    /**
     * 添加action
     */
    public function save(){
        $supplies = input('post.');
        $supplies['sup_createtime'] =  date('Y-m-d H:i:s', time());
        $add = $this->supplies()->add($supplies);
        if(!$add){
            return $this->error(Lang::get('add fail'));
        }
        return $this->success(Lang::get('add success'), 'Supply/index');
    }

    /**
     * 修改action
     */
    public function esave(){
        $supId = input('post.sup_id');
        $supply = $this->supplies()->findById(array('sup_id'=>$supId));
        if(!$supply){
            return $this->error(Lang::get('unfind supply'));
        }
        $post = input('post.');
        $edit = $this->supplies()->update($post, array('sup_id'=>$supId));
        if(!$edit){
            return $this->error(Lang::get('edit fail'));
        }
        return $this->success(Lang::get('edit success'), 'Supply/index');
    }

    public function one(){
        $supId = input('post.sup_id');
        $supply = $this->supplies()->findById(array('sup_id'=>$supId));
        $supply = $this->resetOne($supply);
        echo json_encode($supply);
    }
    /**
     * 删除action
     */
    public function del(){
        $supId = input('post.sup_id');
        $supply = $this->supplies()->findById(array('sup_id'=>$supId));
        if(!$supply){
            return $this->error(Lang::get('unfind supply'));
        }
        $del = $this->supplies()->del(array('sup_id'=>$supId));
        if(!$del){
            return $this->error(Lang::get('del fail'));
        }
        return $this->success(Lang::get('del success'), 'Supply/index');
    }

    /**
     * 搜索action
     */
    public function search(){
        $search = input('param.search');
        $supplies = $this->supplies()->search($search);
        $supplies = $this->resetSupplies($supplies);
        $this->page($supplies);
        $this->asgModelTypes();
        $this->asgCiuFits();
        $this->assign('supplies', $supplies);
        return $this->fetch('sup/index');
    }

    /**
     * 重组-》二维数组
     * @param $supplies
     * @return mixed
     */
    private function resetSupplies($supplies){
        $len = count($supplies);
        for($i=0;$i<$len;$i++){
            $supplies[$i] = $this->resetOne($supplies[$i]);
        }
        return $supplies;
    }

    /**
     * 重组-》一维数组
     * @param $supply
     * @return mixed
     */
    private function resetOne($supply){
        $len = count($supply);
        if($len > 0){
            for($i=0;$i<$len;$i++){
                $fitId = $supply['fit_id'];
                $findFit = $this->fittings()->findById(array('fid'=>$fitId));
                if($findFit){
                    $supply['fitting'] = $findFit['fitting'];
                }else{
                    $supply['fitting'] = '';
                }
            }
        }else{
            $supply['fitting'] = '';
        }
        return $supply;
    }

    /**
     * 显示是CIU的配件
     */
    private function asgCiuFits(){
        $where = array('is_ciu'=>'1');
        $result = $this->fittings()->select('*', $where);
        $this->assign('fittings', $result);
    }

}